//
// Created by admin on 2022/6/16.
// 206. 反转链表: https://leetcode.cn/problems/reverse-linked-list/
//

#include "../DataStructure/ListNode.h"

class ReverseList {
public:
    //头插法
    ListNode* reverseList(ListNode* head) {
        if (head == nullptr || head->next == nullptr) return head;
        auto* dummy = new ListNode(-1);
        ListNode* cur = head;
        dummy->next = head;
        while (cur->next != nullptr) {
            ListNode* node = cur->next;
            cur->next = cur->next->next;
            node->next = dummy->next;
            dummy->next = node;
        }
        return dummy->next;
    }
};
